<?php 
class UserController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index() {
		//
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create() {
		Return View::make("reg");
	}
	/**
	 * Return login page
	 * @return Response
	 */
	public function log(){
		
	}
	/**
	 * Checker for login
	 */
	public function login() {
		$email = Input::get('email');
		$password = Input::get('password');
		$loginarray = array('email' => $email, 'password' => $password);
		if ($res = Auth::attempt($loginarray)) {
			$userinfo = array("email" => Auth::user()->email, "name" => Auth::user()->name);
			$return = array("suc" => true, "userinfo" => $userinfo);
			return Response::json($return);
		}else{
			return Response::json(array("suc"=>false));
		}
	}

	/**
	 * Give the infomation of the request user
	 * to show in the page
	 */
	public function info() {
		if (Auth::check()) {
			$returndata = array("suc" => true, "name" => Auth::user()->name, "email" => Auth::user()->email);
			return Response::json($returndata);
		} else {
			return Response::json(array("suc" => false));
		}
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store() {
		$hasEmailAccount = User::where("email", Input::get('email'))->first();
		if ($hasEmailAccount == null) {
			$cred = array('email' => Input::get('email'), 'password' => Hash::make(Input::get('password')), "name" => Input::get("name"));
			$user = User::create($cred);
			$userinfo = array("id" => $user->id, "email" => $user->email, "name" => $user->name);
			return Response::json(array("suc" => true, "userInfo" => $userinfo));
		} else {
			// 邮箱被占用
			return Response::json(array("suc" => false, "because" => "email-used"));
		}
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int $id
	 * @return Response
	 */
	public function show($id) {
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int $id
	 * @return Response
	 */
	public function edit($id) {
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int $id
	 * @return Response
	 */
	public function update($id) {
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int $id
	 * @return Response
	 */
	public function destroy($id) {
		//
	}

}
